package com;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.gson.Gson;
import algri.AnaSearch;
import db.Search_DB;

/**
 * Servlet implementation class Req_Search
 */
@WebServlet("/Req_Search")
public class Req_Search extends HttpServlet {

	protected void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		PrintWriter out=response.getWriter();
		//获取前端数据 spot_name sel_year sel_month
		String spot_nameString=request.getParameter("spot_name");
		Search_DB searchDB=new Search_DB();
		List<HashMap<String, String>> list=new ArrayList<HashMap<String,String>>();
		list=searchDB.search_results(spot_nameString);
		AnaSearch ana=new AnaSearch();
		response.setHeader("Access-Control-Allow-Origin", "*");
		response.setHeader("Access-Control-Allow-Methods", "Get,Post");
		String zhenji=request.getParameter("inputqualevle");
		System.out.println("传入震级数据:"+zhenji);
		String energy=request.getParameter("inputquaenergy");
		System.out.println(" 传入能量数据:"+energy);
		String start_time=null;
		start_time=request.getParameter("start_time");
		System.out.println("传入开始时间:"+start_time);
		String stop_time=null;
		stop_time=request.getParameter("end_time");
		System.out.println("传入停止时间:"+stop_time);
		List<HashMap<String, String>> coordinate_list=new ArrayList<HashMap<String,String>>();
		coordinate_list.clear();
		
		
				//假设未输入时间数据的情况（起始时间和终止时间有一个未输入的就认为没有进行时间限定）
		if (start_time.length()==0||stop_time.length()==0) {
			//仅按照能量查询
			if (zhenji.length()==0&&energy.length()!=0) {
				try {
					coordinate_list=ana.reqe_coordinate(list,energy);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			//仅按照震级查询
			if (zhenji.length()!=0&&energy.length()==0) {
				try {
					coordinate_list=ana.reql_coordinate(list, zhenji);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
			if(zhenji.length()!=0&&energy.length()!=0){
				try {
					coordinate_list=ana.reqel_coordinate(list, zhenji, energy);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		}
		
		//else 时间条件输入了，
		else {
			//震级未输入  按照时间＋能量进行查询
			//能量未输入，按照时间加震级进行查询
			if (zhenji.length()==0&&energy.length()==0) {
				//执行仅有起止时间的方法
				try {
					coordinate_list=ana.reqtime_coordinate(list, start_time, stop_time);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			else if (zhenji.length()==0&&energy.length()!=0) {
				//执行 带有起止时间和能量的方法
				try {
					coordinate_list=ana.reqte_coordinate(list, start_time, stop_time, energy);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			
			else if (zhenji.length()!=0&&energy.length()==0) {
				try {
					coordinate_list=ana.reqtl_coordinate(list, start_time, stop_time, zhenji);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
			
			
			else {
				try {
					coordinate_list=ana.reqall_coordinate(coordinate_list, zhenji, energy, start_time, stop_time);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	

		Gson gson=new Gson();
		String jsonString=gson.toJson(coordinate_list);
		System.out.println("查询结果共计："+coordinate_list.size()+"条");
		System.out.println(jsonString);
		out.write(jsonString);
		out.flush();
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doGet(request, response);
	}

}
